(post
 :title "State of data-binding (MVVM) in Kivy and Android"
 :date (make-date* 2016 9 23)
 :tags '("data" "data-binding" "kivy" "python" "android" "sdk" "mvvm")
 (h4 [TLDR: Data-binding support is an essential component in the MVVM
            framework. While android has recently adopted data-binding, Kivy has always long had excellent data-binding support])
 (h3 [MVVM and data-binding])
 (p [(Skip if familiar) Application architectures like MVVM
     (Model, View, View-Model) exist to aid in separating view logic from
     business logic. What makes
     MVVM different, is its focus on binding data between the view and the
     view-model. What this means, is that data in the view and in the
     view-model are tightly coupled, in that any registerable event in the
     view, is immediately recognized by the view-model. Additionally, the
     view-model has hooks into the model, so it can format and send any
     changed model data directly to the view.])
 (h3 [Android and MVVM])
 (p [Android was designed, unlike iOS, to be architecture independent.
             For many small applications, this is preferable, since setting up
             MVVM requires many more files and added complexity.
             However, as an application continues
             to grow, it's easy for Android code can get messy. In particular,
             Activities, which already deal with life-cycles, and broadcast
             listeners, also has to manage view events from the view.
             Interacting with the view in activities
             makes for ugly code too: after typecasting each widget we want to
             interact with
             after finding it by id, even simple things like setting text or
             responding to a click
             event are awfully verbose.
             (Place example code here)
             Worse yet, since we are working directly with activities, nearly every
             test requires the full SDK,
             which implies we are stick with slow emulation testing.
             (Maybe a link here?)])
 (p [Clearly there was room for improvement, and a number of packages were since
             developed to address these issues. DI frameworks like ButterKnife (link)
             handle automatic view binding, and RoboBindings (link) allow view logic
             to be handled in a separate ViewModel class.
             ])
 (p [Since DI is separate software concept, let's instead focus on RoboBindings.
           RoboBindings is a great library because
           it allows for strong separation of concerns.
           Now, all the view logic originally in the Activity is implemented in a
           separate ViewModel class, freeing the Activity to focus on its main
           goals: controlling lifecycles, and listening for app/system-wide broadcasts.
           And unlike before, the viewmodel and view directly share the same data,
           and agree on an API to handle events like button clicks. Overall, this makes
           for much cleaner view logic code. Event listeners are gone, since the
           view chooses how to delegate events to the view-model, and in the view-model
           we don't have to manually update any view widget since any data change
           in the view-model automatically updates the view!])
 (p [Yeah, data-binding is pretty damn genius. Just one last point: the viewmodel
           can be any standard POJO (plain old java object) which allows native
           unit testing.])
 (p [Robobinding is of course a 3rd party library, which is somewhat risky for a
                 large app developer that needs reliability. Fortunately, Android
                 has since Marshmallow, provided its own databinding library: (link
                                                                               here)
                 While I still personally use robobinding, it looks solid, and
                 follows a similar approach])
 
 (h3 [Okay so what is/why Kivy?])
 (p [I have a feeling y'all are less likely to have heard of Kivy. Essentially
       it's a well-established cross platform application framework written
       in Python. It was originally designed to run on large multi-touch
       screens, but has since been ported to run on Android and iOS. One of
       it's coolest features is kv-lang, an alternative to XML seen in Android,
       that makes it really easy to build a widget tree, with access to
       the full power of Python. In addition it has amazing data-binding
       support.])
 (p [Since we are familiar with what data-binding looks like in Android, let's
           get right to some code.])
 (ul
  (li [calling a proc in kv:])
  (li [binding an item in kv to field in python:])
  (li [hooks for receiving event from view and updating view:])
  (li [managing simple view logic within the view:])))
